<template>
  <div class="manager">
    <!-- 工具栏 -->
    <div class="toolbar">
      <div class="left">
        <el-button type="primary" icon="el-icon-plus">添加</el-button>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="tablebox">
      <el-table border stripe :data="tableData">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="用户名" prop="name"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="状态" prop="status"></el-table-column>
        <el-table-column label="头像" prop="avatar"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域-->
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 对话框区域 -->
    <div class="diagbox">
      <el-dialog title="编辑管理员" width="40%" center :visible.sync="editDiaShow" @close="handleClose">
        <div class="eidtbox">
          <el-form :model="managerObj" label-width="80px" :rules="rules" ref="editForm">
            <el-form-item label="用户名:" prop="name">
              <el-input v-model="managerObj.name"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:" prop="email">
              <el-input v-model="managerObj.email"></el-input>
            </el-form-item>
            <el-form-item label="状态:">
              <el-radio-group v-model="managerObj.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="0">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="sureDialog">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      editDiaShow: false,
      currentPage: 1, //当前页
      pageSize: 10, //每页条数
      total: 400, //总条数
      managerObj: {
        userId: 1,
        name: "张三",
        email: "zs@qq.com",
        status: 1
      },
      tableData: [
        {
          id: 1,
          name: "张三",
          email: "zs@qq.com",
          status: 1,
          avatar: "a.jpg",
          createTime: "2019-11-1"
        }
      ],
      rules: {
        name: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    //编辑按钮
    handleEdit(index, item) {
      this.editDiaShow = true;
    },
    //删除按钮
    handleDelete(index, item) {
      this.$confirm("你确定要删除吗，请三思,后果自负", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log("确定了，要删除");
        })
        .catch(() => {
          console.log("放弃了");
        });
    },
    //切换每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
    },
    //切换分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    // 关闭弹框
    handleClose() {
      console.log("关闭之前");
      this.$refs.editForm.resetFields();
      this.editDiaShow = false;
    },
    //确认弹框
    sureDialog() {
      // console.log("编辑后的数据", this.editObj);
      // this.editDiaShow = false;
      this.$refs.editForm.validate(valid => {
        if (valid) {
          console.log("验证通过");
          //弹出消息提示
          this.$message({
            message: "恭喜你，这是一条成功消息",
            type: "success",
            duration: 1000
          });
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.manager {
  width: 100%;
  min-height: calc(100vh - 98px);
  background: #fff;
  padding: 15px;
  .toolbar {
    margin-bottom: 20px;
    overflow: hidden;
    .left {
      float: left;
    }
  }
  .page {
    text-align: center;
    margin-top: 20px;
  }
  .eidtbox {
    width: 400px;
    margin: 0 auto;
  }
}
</style>